/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2p.raca.Client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author p
 */
public final class ListaFileClient {
    
    public static Vector fileList = null;
    public static Vector fileSizes = null;
    
    public ListaFileClient()
    {
        fileList = new Vector();
        fileSizes = new Vector();
        try {
            updateFileList();
            LoadLista();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }
        public void SaveLista() throws IOException
    {
        //LoadLista();
        File arquivo = new File("ListaFile.txt");
        if (arquivo.exists())
            arquivo.delete();
        arquivo.createNewFile();
        FileWriter fw = new FileWriter(arquivo, false);
        PrintWriter out = new PrintWriter(fw);
        for (int i=0; i<fileList.size();i++)
        {
            out.println(fileList.elementAt(i));
            out.flush();
            out.println(fileSizes.elementAt(i));
            out.flush();
        }
                
        
    }
        public void PrintLista()
        {
            for (int i=0;i<fileList.size();i++)
                System.out.println("Arquivo: "+fileList.elementAt(i)+"  tamanho: "+ fileSizes.elementAt(i));
                
        }
        public void updateFileList()
        {
             File file = new File("shared"); 
             File afile[] = file.listFiles();
             fileList = new Vector();
             
        for (int j =0;j< afile.length; j++)   
        {  
            File arquivo = afile[j];
            fileList.add(arquivo.getName().substring(0,arquivo.getName().length()-4));
            fileSizes.add(arquivo.length());
        } 
        try {
            SaveLista();
        } catch (IOException ex) {
        }
        }
        public void LoadLista() throws FileNotFoundException, IOException
        {
            fileList = new Vector();
            fileSizes = new Vector();
            //File arquivo = new File("ListaFileClient.txt");

            BufferedReader in = new BufferedReader(new FileReader("ListaFile.txt"));

            while(in.ready())
            {
                addElem(in.readLine(),fileList);
                addElem(in.readLine(),fileSizes);
            }
            in.close();
        }
    
    public void addElem(String nome, Vector v)
    {
        v.add(nome); 
    }
    
    public void removeElem(String nome, Vector v) throws IOException
    {
        int i=0;
        while (i<v.size())
        {
            if (nome.equalsIgnoreCase((String)v.elementAt(i)))
            {
                v.remove(i);

               }
            i++;
        }
        SaveLista();
    }
}
